<template>
  <el-container class="home-container">
    <el-header>
      <div class="home-logo">
        <img src=../heima.png>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="tuichu">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧菜单栏开始 -->
      <el-aside :width="suoJing?'64px':'200px'">
        <!-- 缩进栏 -->
        <div class="toggle-button" @click="suoJin">|||</div>
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#409BFF"
          unique-opened
          :collapse="suoJing"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i class="el-icon-s-tools"></i>
              <!-- 一级菜单文字 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
              @click="activePath('/'+item2.path)"
            >
              <!-- 二级菜单的模板 -->
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单文字 -->
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 左侧菜单栏结束 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      active: '',
      myIcon: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      suoJing: false
    }
  },
  created() {
    this.getData()
    // 页面刷新自动高亮当前的索引
    this.active = sessionStorage.getItem('active')
  },
  methods: {
    // 退出删除token
    tuichu() {
      // 删除token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
      // 退出提示
      this.$message.error('退出登录成功')
    },
    // 获取左侧菜单信息
    async getData() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.erro(res.meta.msg)
      this.menulist = res.data
    },
    // 点击缩进
    suoJin() {
      this.suoJing = !this.suoJing
    },
    activePath(active) {
      // 鼠标点击的时候把值传递了
      this.active = active
      // 同时把值存起来了,刷新的时候用到
      sessionStorage.setItem('active', active)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  .home-logo {
    display: flex;
    align-items: center;
    img {
      margin-right: 15px;
    }
  }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  line-height: 24px;
  font-size: 10px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
